package com.example.zhxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhxy.mapper.StudentMapper;
import com.example.zhxy.pojo.Clazz;
import com.example.zhxy.pojo.LoginForm;
import com.example.zhxy.pojo.Student;
import com.example.zhxy.service.ClazzService;
import com.example.zhxy.service.StudentService;
import com.example.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private ClazzService clazzService;
    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        Student student = this.baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public IPage IPage(Page<Student> page, Student student) {
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(student.getName())){
            queryWrapper.eq("name",student.getName());
        }if(!StringUtils.isEmpty(student.getClazzName())){
            queryWrapper.like("clazz_name",student.getClazzName());
        }
        queryWrapper.orderByDesc("id");
        Page<Student> page1 = baseMapper.selectPage(page, queryWrapper);
        return page1;

    }

    @Override
    public void addOrUpdateStudent(Student student) {
        if(student.getId()==null){
            boolean save = this.save(student);
            if(save){
                QueryWrapper<Clazz> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("name",student.getClazzName());
                Clazz one = clazzService.getOne(queryWrapper);
                one.setNumber(one.getNumber()+1);
                clazzService.updateById(one);
            }
        }else {
            this.updateById(student);
        }

    }

    @Override
    public boolean delStudentById(List<Integer> list) {
        for (Integer integer : list) {
            Student byId = this.getById(integer);
            boolean b = this.removeById(integer);
            if(b){
                QueryWrapper<Clazz> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("name",byId.getClazzName());
                Clazz one = clazzService.getOne(queryWrapper);
                one.setNumber(one.getNumber()-1);
                clazzService.updateById(one);
            }else {
                return false;
            }
        }
        return true;
    }


}
